package cn.itcast.web.controller.dept;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;

import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @RequestMapping("list")
    public String findByPage(@RequestParam(defaultValue = "1") Integer pageNum ,@RequestParam(defaultValue = "4") Integer pageSize){
        PageInfo<User> pageInfo = userService.findByPage(getLoginCompanyId(), pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "system/user/user-list";
    }


    /**
     * 功能入口：/system/dept/list
     * 请求地址：toAdd
     * 请求参数：
     * 响应数据：system/user/user-add
     *
     * @param
     * @return
     */
    @RequestMapping("toAdd")
    public String add(){
        //显示下拉框数据
        List<Dept> deptList = deptService.findDeptList(getLoginCompanyId());

        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    /**
     * 添加与修改，保存数据
     * 功能入口：system/user/user-add
     * 请求地址： /system/user/edit.do
     * 请求参数：User  user
     * 响应地址：system/user/user-list
     */

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("edit")
    public String edit(User  user){
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(user.getId())){
            userService.save(user);
            //添加用户成功，发送邮件
            if (user.getEmail()!=null && !"".equals(user.getEmail())){

                //处理发送邮件的任务
                String title="新员工入职通知";
                String content="欢迎来到SaasExport团队";
                //发送消息
                Map<String ,String>map=new HashMap<>();
                map.put("email",user.getEmail());
                map.put("title",title);
                map.put("content",content);
                rabbitTemplate.convertAndSend("msg.email",map);
            }
        }else {
            //修改方法1.保存数据
            userService.update(user);
        }
        return "redirect:/system/user/list";
    }


    //2.修改回显数据
    @RequestMapping("toUpdate")
    public String update(String id){
        //1.回显所有的数据
       User user= userService.findById(id);
       //2.修改方法的下拉框数据
        List<Dept> deptList = deptService.findDeptList(getLoginCompanyId());

        request.setAttribute("user",user);
        request.setAttribute("deptList",deptList);

        return "system/user/user-update";
    }



    //删除方法
    /**
     * 方法入口：/system/user/list
     * 请求参数：id
     * 请求路径：/system/user/delete.do
     * 响应路径：/system/user/list
     */

    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        HashMap<String, Object> result = new HashMap<>();
        boolean flag=userService.delete(id);
        if (flag){
            result.put("message","1");
        }else {
            result.put("message","0");
        }
        return result;
    }

    /**
     * 功能入口："roleList"
     * 方法参数：id
     * 返回地址：list
     *
     * @return
     */
    @RequestMapping("roleList")
    public String roleList(String id){
        //根据用户id查询显示
        User user = userService.findById(id);
        //查询所有的角色
        List<Role> roleList=roleService.findAll(getLoginCompanyId());
        //查询用户已经拥有的角色
        List<Role>userRoleList =roleService.findRoleByUserId(id);
        request.setAttribute("user",user);
        request.setAttribute("roleList",roleList);
        //遍历用逗号隔开获取到的Id,前端判断自动获取
//        String roleIds="";
//        for (Role role : userRoleList) {
//            roleIds+=role.getId()+",";
//        }
        request.setAttribute("userRoleList",userRoleList);


        return "/system/user/user-role";
    }

    //角色保存
    /**
     * 功能入口：/system/user/user-role
     * 请求地址：/system/user/changeRole.do
     *请求参数：userid  roleIds
     * 响应地址：重定向：/system/user/list
     */
    @RequestMapping("changeRole")
    public String changeRole(String userId,String[] roleIds ){
        userService.changeRoleSave(userId,roleIds);
        return "redirect:/system/user/list";
    }

}
